<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('FeedbackFormHandler')) return;
/**
 * Feedback plugin
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage feedback
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

/**
 *  Forms handler
 *  This class contains common operations for feedback forms handling
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com) 
 */
class FeedbackFormHandler {
    private $formName;
    private $config;
    
    public function __construct($form_name){
        $this->formName = $form_name;
        include_spip('formulaires/'.$this->formName.'/config');
        $this->config = $GLOBALS['config_formulaire_'.$this->formName];
    }
    
    /**
     *  Uploads files for current form
     *  This function will upload all files found in $_FILES array and put all their data in $_GLOBALS array, so later it can be used in FeedbackEmail class
     *  
     *  @param string $error Error of uploading process. If empty, all is OK
     */
    public function UploadFiles(&$error) {        
        include_spip('classes/pdk_upload');
    
        $config = array();
        $config['upload_path'] = isset($this->config['attach_files']['upload_path']) && $this->config['attach_files']['upload_path'] ? $this->config['attach_files']['upload_path'] : _DIR_TMP;
        $config['allowed_types'] = isset($this->config['attach_files']['allowed_types']) && $this->config['attach_files']['allowed_types'] ? $this->config['attach_files']['allowed_types'] : '*';        
        $config['max_size'] = isset($this->config['attach_files']['max_size']) && $this->config['attach_files']['max_size'] ? $this->config['attach_files']['max_size'] : 2048;
        $config['encrypt_name'] = isset($this->config['attach_files']['encrypt_name']) && $this->config['attach_files']['encrypt_name'] ? true : false;
        $uploader = new pdk_Upload($config);
            //var_dump($_FILES); exit;
        if ($_FILES && $this->config['attach_files']['allow_attach'] && $this->CheckFiles($error))
            foreach ($_FILES as $file=>$data) {
                if (!$data['name']) continue; // Left out empty file fields
                if ($uploader->do_upload($file)) {
                    $file_data = $uploader->data();
                    // Save file data into globals, so later we can use it in traiter function 
                    $GLOBALS['uploaded_files_data_'.$this->formName][$file] = $file_data;
                } else {                    
                    $error = _T("feedback:file_load_error");
                    //TODO: Удалить успешно загруженные файлы в случае ошибки
                }     
            }
    }
    
    /**
     * Checks uploaded files
     * @param string $error Error of uploading process. If empty, all is OK
     * @return bool True if OK, False otherwise
     */
    public function CheckFiles(&$error) {
        $min_files = $this->config['attach_files']['min_files'];
        if (!isset($min_files)) $min_files = 0;
        $uploaded = 0;
        foreach ($_FILES as $file) {
            if ($file['name'])
                $uploaded++;
        }
        if ($uploaded < $min_files) {
            $error = _T('feedback:min_files_must_be_loaded').': '.$min_files;
            return false;
        }
        
        return true;
    }
} 
?>